#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

#define in read()
#define pii pair<int,int>
#define fi first
#define se second
#define FILE(x) freopen(x".in","r",stdin);\
	freopen(x".out","w",stdout);
#define pb push_back

ll read(){
	ll x = 0,sgn = 1;char ch = getchar();
	for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;
	for(;isdigit(ch);ch = getchar()) x = (x<<1)+(x<<3)+(ch^48);
	return x*sgn;
}

const int N = 2e5+10;

ll n,m,k[N],suf,maxs[N],s[N],ms;
char T[N];

int main (){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in;
	for(int i = 1;i <= m;i++){
		scanf("%s",T + 1); int l = strlen(T + 1); k[i] = in;
		for(int j = l;j >= 1;j--){
			int t = T[j] == 'F' ? -1 : 1;
			s[i] += t; maxs[i] = max(maxs[i],s[i]);
		}
	}
	ms = 1;
	for(int i = m;i >= 1;i--){
		if(s[i] > 0) ms = max(ms,suf + (k[i] - 1) * s[i] + maxs[i]);
		else ms = max(ms,suf + maxs[i]);
		suf += k[i] * s[i];
	}
	if(suf >= 2) return puts("-1"),0;
	printf("%lld\n",ms - 1);
	return 0;
}

